package com.example.demo.service;

import com.example.demo.DTO.userDTO;
import com.example.demo.dao.*;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.converter.userConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.List;

@Service
public class userServiceImpl implements userService {

    @Autowired
    private userRepository userRepository;
    @Override
    public userDTO getUserById(long id) {
        user user = userRepository.findById(id).orElseThrow(RuntimeException::new);
        return userConverter.convertTest(user);
    }

    @Override
    public user addNewUser(userDTO userDTO){//用于注册
        List<user> userList = userRepository.findByNameOrPhoneOrEmail(userDTO.getName(),userDTO.getPhone(),userDTO.getEmail());
        if (!CollectionUtils.isEmpty(userList)){
            throw new IllegalStateException("注册信息已被占用");
        }
        user user = userRepository.save(userConverter.convertTest(userDTO));
        return user;
    }

    @Override
    public void deleteUserById(long id) {
        userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("id:" + id + "不存在"));
        userRepository.deleteById(id);
    }

    @Override
    @Transactional
    public user updateStateById(long id) {
        user userInDB = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("id:" + id + "不存在"));
        userInDB.setUserState("yes");
        System.out.println(userInDB.getUserState());
        user user = userRepository.save(userInDB);
        return user;
    }

    @Override
    @Transactional
    public userDTO updateUserEmailById(long id, String email) {
        user userInDB = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("email:" + email + "不存在"));
        if (StringUtils.hasLength(email) && !userInDB.getEmail().equals(email)){
            userInDB.setEmail(email);
        }
        user user = userRepository.save(userInDB);
        return userConverter.convertTest(user);
    }

    @Override
    public user getUserByNameAndPass(String name, String pass) {//登陆时匹配用户名与密码
        user userdb = userRepository.findByNameAndPass(name,pass);
        System.out.println(userdb);
        return userdb;
    }

    @Override
    public user getUserByName(String name) {//查询用户信息
        System.out.println(name);
        user userdb = userRepository.findByName(name);
        return userdb;
    }

    @Override
    public List<user> getUserByState(String userState) {
        List<user> userdb = userRepository.findByUserState(userState);
        return userdb;
    }

    @Override
    public user updateMoneyByName(String name, int money) {
        user userdb = userRepository.findByName(name);
        userdb.setMoney(money);
        user user = userRepository.save(userdb);
        return user;
    }
}
